package com.bzkj.web.controller.basic;

import com.alibaba.fastjson2.JSONObject;
import com.bzkj.basicinfo.domain.fieldTranslation.FieldTranslation;
import com.bzkj.basicinfo.service.fieldTranslation.IFieldTranslationService;
import com.bzkj.common.annotation.Log;
import com.bzkj.common.annotation.LogC;
import com.bzkj.common.core.controller.BaseController;
import com.bzkj.common.core.domain.AjaxResult;
import com.bzkj.common.core.page.TableDataInfo;
import com.bzkj.common.enums.BusinessType;
import com.bzkj.common.utils.poi.CsvExportUtils;
import com.bzkj.common.utils.poi.ExcelPDFUtil;
import com.bzkj.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 字段翻译映射Controller
 *
 * @author bzkj
 * @date 2025-03-21
 */
@RestController
@RequestMapping("/basic/field/translation")
public class FieldTranslationController extends BaseController
{
    @Autowired
    private IFieldTranslationService fieldTranslationService;

    /**
     * 查询字段翻译映射列表
     */
    @PreAuthorize("@ss.hasPermi('basic:translation:list')")
    @GetMapping("/list")
    public TableDataInfo list(FieldTranslation fieldTranslation)
    {
        startPage();
        List<FieldTranslation> list = fieldTranslationService.selectFieldTranslationList(fieldTranslation);
        return getDataTable(list);
    }

    /**
     * 导出字段翻译映射列表
     */
    @PreAuthorize("@ss.hasPermi('basic:translation:export')")
    @Log(title = "字段翻译映射", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(HttpServletResponse response, FieldTranslation fieldTranslation)
    {
        List<FieldTranslation> list = fieldTranslationService.selectFieldTranslationList(fieldTranslation);
        ExcelUtil<FieldTranslation> util = new ExcelUtil<>(FieldTranslation.class);
        util.exportExcel(response, list, "字段翻译映射数据");
        return AjaxResult.success();
    }

    /**
     * 导出字段翻译映射列表
     */
    @PreAuthorize("@ss.hasPermi('basic:translation:export')")
    @Log(title = "字段翻译映射", businessType = BusinessType.EXPORTPDF)
    @PostMapping("/exportPdf")
    public AjaxResult exportPdf(HttpServletResponse response, FieldTranslation fieldTranslation)
    {
        List<FieldTranslation> list = fieldTranslationService.selectFieldTranslationList(fieldTranslation);
        ExcelPDFUtil<FieldTranslation> util = new ExcelPDFUtil<FieldTranslation>(FieldTranslation.class);
        return util.exportPDF(response, list, "字段翻译映射数据","字段翻译映射数据",getUsername(),new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()),"全量");
    }

    /**
     * 导出字段翻译映射列表
     */
    @PreAuthorize("@ss.hasPermi('basic:translation:export')")
    @Log(title = "字段翻译映射", businessType = BusinessType.EXPORTCSV)
    @PostMapping("/exportCsv")
    public AjaxResult exportCsv(HttpServletResponse response, FieldTranslation fieldTranslation) {
        List<FieldTranslation> list = fieldTranslationService.selectFieldTranslationList(fieldTranslation);
        try {
            CsvExportUtils.exportToWeb(response, "字段翻译映射.csv", list);
            return AjaxResult.success("导出成功");
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.success("导出失败");
        }
    }
    /**
     * 获取字段翻译映射详细信息
     */
    @PreAuthorize("@ss.hasPermi('basic:translation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(fieldTranslationService.selectFieldTranslationById(id));
    }

    /**
     * 新增字段翻译映射
     */
    @PreAuthorize("@ss.hasPermi('basic:translation:add')")
    @PostMapping
    @LogC(title = "字段翻译映射", businessType = BusinessType.INSERT, pkFieldNames = "id",
            tableNames = "field_translation",content = "' 字段翻译映射：'+#fieldTranslation.name"
    )
    public AjaxResult add(@RequestBody FieldTranslation fieldTranslation)
    {
        return toAjax(fieldTranslationService.insertFieldTranslation(fieldTranslation));
    }

    /**
     * 修改字段翻译映射
     */
    @PreAuthorize("@ss.hasPermi('basic:translation:edit')")
    @PutMapping
    @LogC(title = "字段翻译映射", businessType = BusinessType.UPDATE, pkFieldNames = "id",
            tableNames = "field_translation"
    )
    public AjaxResult edit(@RequestBody FieldTranslation fieldTranslation)
    {
        return toAjax(fieldTranslationService.updateFieldTranslation(fieldTranslation));
    }

    /**
     * 删除字段翻译映射
     */
    @PreAuthorize("@ss.hasPermi('basic:translation:remove')")
    @Log(title = "字段翻译映射", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @LogC(title = "字段翻译映射", businessType = BusinessType.INVALID)
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(fieldTranslationService.deleteFieldTranslationByIds(ids));
    }

    /**
     * 查询表的基础信息和字段信息
     * @param tableName 表名
     * @return 查询结果
     */
    @GetMapping("/table/{tableName}")
    public AjaxResult getTableInfoAndFieldInfo(@PathVariable("tableName") String tableName){
        if (tableName == null || tableName.trim().isEmpty()){
            return error("请选择需要查询的表名！");
        }
        return fieldTranslationService.getTableInfoAndFieldInfo(tableName);
    }

    /**
     * 导入字段翻译
     * @param resultBody 待导入的数据
     * @return 导入结果
     */
    @PostMapping("/import")
    @LogC(title = "字段翻译映射", businessType = BusinessType.IMPORT)
    public AjaxResult importFieldTranslation(@RequestBody String resultBody){
        JSONObject resultJsonObject = JSONObject.parseObject(resultBody);
        return fieldTranslationService.importFieldTranslation(resultJsonObject);
    }
}
